@extends('layouts.app')

@section('content')
<div class="content order content_import">
	<div class="button">
		<span class="business">
			@if(count($business) == 1)
			<label>所有商家</label><i></i>
			<ul>
				<li data-activity="{{ json_encode($business[0]->activitys->load(['types'=>function($query) use ($business){
					return $query->where('business_id',$business[0]->id);
				}])->toArray()) }}" business="{{$business[0]->id}}">{{$business[0]->name}}</li>
			</ul>
			@else
			<label>所有商家</label><i></i>
			<ul>
				@foreach($business as $item)
				<li data-activity="{{ json_encode($item->activitys->load(['types'=>function($query) use ($item){
					return $query->where('business_id',$item->id);
				}])->toArray()) }}">{{$item->name}}</li>
				@endforeach
			</ul>
			@endif
		</span>
		<span class="activity">
			<label>所有活动</label><i></i>
			<ul>
			</ul>
		</span>
		<span class="channel">
			<label>所有渠道</label><i></i>
			<ul>
			</ul>
		</span>
		<label for="">导入时间：</label>
		<input id="start" type="text" placeholder="开始时间" readonly>
		<input id="end" type="text" placeholder="结束时间" readonly>
		<span class="order_type" data-type="order_id" style="margin-right: 0;border-radius:4px 0 0 4px;">
			<label>订单号</label><i></i>
			<ul>
				<li data-type="order_id">订单号</li>
				<li data-type="phone">手机号</li>
				<li data-type="contacts">联系人</li>
			</ul>
		</span>
		<input class="order" type="text"><button class="order_search" onclick="search(this)">搜索</button>
		<p>统计：当前已导入订单数为<label for="" id="orderNum">0</label>个，已发送短信的订单数为<span id="sendOrderNum">0</span>个。</p>
		<div><label for="" id="_all">全选/全不选</label><span id="import">导入</span> | <span class="export">导出</span> | <span class="delete">删除</span></div>
	</div>
	<table>
		<thead>
			<tr>
				<th style="text-align: right;">序号</th><th>订单号</th><th>联系人</th><th>联系电话</th>
				<th>订单金额</th><th>导入时间</th><th>状态</th><th>操作</th>
			</tr>
		</thead>
		<tbody id="list">

		</tbody>
	</table>
	<div class="page_item">
		<div class="page_num">
			<label for="" class="active">10</label>
			<label for="">30</label>
			<label for="">50</label>
		</div>
		<ul class="pagination" id="page1">
		</ul>
		<div class="pageJump">
			<span>跳转到</span>
			<input type="text"/>
			<span>页</span>
			<button type="button" class="button">确定</button>
		</div>
	</div>
</div>
@endsection
@section('scripts')
<script type="text/javascript">

	var baseUrl = 'orders';

	params['page'] = 1;
	params['pageSize'] = $('.page_num').find('.active').text();

	getList(baseUrl,params);
	//订单导入时间插件
	var start = {
		elem: '#start',
		format: 'YYYY-MM-DD',
		max: '2099-06-16', //最大日期
		istime: false,
		istoday: false,
		choose: function(datas){
		    end.min = datas; //开始日选好后，重置结束日的最小日期
		    end.start = datas //将结束日的初始值设定为开始日
		}
	};
	var end = {
		elem: '#end',
		format: 'YYYY-MM-DD',
		max: '2099-06-16',
		istime: false,
		istoday: false,
		choose: function(datas){
		    start.max = datas; //结束日选好后，重置开始日的最大日期
		}
	};
	laydate(start);
	laydate(end);

	$(function(){
		$('.page_content').on('click','#import',function(){
			$.ajax({
				type: "GET",
				url: "/order/import",
				dataType: "html",
				success: function(res) {
					art.dialog({
						title: '导入订单',
						width: '497px',
						lock: true,
						id: 'relation',
						content: res,
					});
				}
			});
		})
		//订单导出/删除
		$('.content_import .export').click(function(){
			var data=new Array;
			var tr=$('.content_import>table>tbody>tr');
			for (var i = tr.length - 1; i >= 0; i--) {
				if (tr[i]['children'][0].attributes.class.value.indexOf('active')>0) {
					data.push(tr[i]['children'][0].attributes['data-id'].value);
				}
			}
			if(data != 'all' && !data.length){
				box.tipHover({ status: 0, msg: "没有选中任何数据" });
				// alert('没有选中任何数据');
				return false;
			}
			window.open('/export/'+data);
		});


		$('.content_import .delete').click(function(){
			var data=new Array;
			var tr=$('.content_import>table>tbody>tr');
			for (var i = tr.length - 1; i >= 0; i--) {
				if (tr[i]['children'][0].attributes.class.value.indexOf('active')>0) {
					data.push(tr[i]['children'][0].attributes['data-id'].value);
				}
			}
			if(data != 'all' && !data.length){
				box.tipHover({ status: 0, msg: "没有选中任何数据" });
				// alert('没有选中任何数据');
				return false;
			}
			$.ajax({
				type: "DELETE",
				url: "/order/"+data,
				dataType: "json",
				success: function(res) {
					if(res.code != 0) {
						box.tipHover({ status: 0, msg: res.msg });
						// alert(res.msg)
					}else{
						box.tipHover({ status: 0, msg: "删除成功" });
						// alert('删除成功');
						getList(baseUrl,params);
					}
				}
			});
		});

		$('body').on('click','.order_import_box .channel li',function(){
			var id=$(this).attr('map');
			getRelations(id);
		})

		$('body').on('click','.import_exception_box .export-error',function(){
			window.open('/exportError');
		});

		$('.page_item>.page_num>label').click(function(){
			params['pageSize'] = $('.page_num').find('.active').text();
			params['page'] = 1;
			getList(baseUrl,params);
		})
	})

	function getRelations(id){
		$.ajax({
			type: "GET",
			url: "/relation/"+id,
			dataType: "html",
			success: function(res) {
				$('#relation').html(res);
			}
		});
	};

	function search(_this) {
		var business=$(_this).parents('.button').find('.business').attr('business');
		var activity=$(_this).parents('.button').find('.activity').attr('activity');
		var map=$(_this).parents('.button').find('.channel').attr('map');
		var start_time=$(_this).parents('.button').find('#start').val();
		var end_time=$(_this).parents('.button').find('#end').val();
		var searchKey=$(_this).parents('.button').find('.order_type').attr('data-type');
		var searchValue=$(_this).parents('.button').find('.order').val();
		params.business = business ? business : '';
		params.activity = activity ? activity : '';
		params.map = map ? map : '';
		params.start_time = start_time ? start_time : '';
		params.end_time = end_time ? end_time : '';
		params.searchKey = searchKey ? searchKey : '';
		params.searchValue = searchValue ? searchValue : '';

		params.page = 1;
		getList(baseUrl,params);
	}

	function submit_excel(){
		var form = $('#submit_excel');
		var url = form.attr('action');
		$("#submit_excel").ajaxSubmit({
            type: "post",
            dataType: "json",
            url: url,
            beforeSend: function(){
	    		$("body").append('<div class="loading" style="width: 100%;height: 100%;position: fixed;z-index: 1991;top: 0px;left: 0px;overflow: hidden;background: rgba(0, 0, 0, 0.5);"><div style="width:200px;height:100px;position:fixed;top:50%;margin-top:-50px;margin-left:-100px;left:50%;color:#fff;font-size:24px;text-align:center;line-height:100px;">loading...</div></div>');
    		},
    		complete: function(){
    			setTimeout(function(){$('.loading').remove();},450);
		    },
            success: function (res) {
            	if(res.code == 0){
            		if (res.html) {
            			art.dialog.list['import'].close();
		    			art.dialog({
							title: '导入异常',
							width: '600px',
							id: 'export',
							lock: true,
							content: res.html,
						});
            		}else{
            			window.location.reload();
            		}
	    		}else{
	    			box.tipHover({ status: 0, msg: res.msg });
					// alert(res.msg);
	    		}
            },
            error: function (msg) {
            }
    	});
    	return false;
	}

	//添加文件
	function show(index){
		$('#upload_file').next().text(index.value);
	}

</script>
@endsection